<?php
class M_categories extends CI_Model{
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    public function add_categories($cat_name) {
        
        //$query = "INSERT INTO categories (user_id, cat_name) values (1, '$cat_name')";
        $data = array(
            'user_id'=>1,
            'cat_name'=>$cat_name
        );
        $result = $this->db->insert('categories',$data);
        //$result = $this->db->get('categories');
        return $result; 
    }
    public function view_categories(){
//        $query = "SELECT * FROM categories";
//        $result = $this->db->query($query);
        $result = $this->db->get('categories');
        return $result->result_array();
    }
    public function view_category($cat_id){
//        $query = "SELECT * FROM categories WHERE cat_id ='$cat_id'";
//        $result = $this->db->query($query);
        //$this->db->where();
        $result = $this->db->get_where('categories',array('cat_id' => $cat_id));
        return $result->result_array();
    }
    
    public function view_category2($cat_name){
//        $query = "SELECT * FROM categories WHERE cat_name ='$cat_name'";
//        $result = $this->db->query($query);
        $result = $this->db->get_where('categories',array('cat_name' => $cat_name));
        return $result->result_array();
    }
    public function update_category($cat_name, $cat_id){
        
//        $query = "UPDATE categories SET cat_name = '{$cat_name}' WHERE cat_id = '{$cat_id}'";
//        $result = $this->db->query($query);
        $data = array(
            'cat_name'=>$cat_name,
            'cat_id'=>$cat_id
        );
       // $this->db->where('cat_id',$cat_id);
        $result = $this->db->update('categories', $data,array('cat_id'=>$cat_id));
        return $result;
    }
    public function delete($cat_id){
         
//        $query = "DELETE FROM categories WHERE cat_id = '$cat_id' LIMIT 1";
//        $result = $this->db->query($query);
        $result = $this->db->delete('categories', array('cat_id'=>$cat_id));
        return $result;
    }
}
?>
